package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianying.comment.CommonResult;
import com.qianying.dto.ShopUserInfoDTO;
import com.qianying.entity.ShopUserInfo;
import com.qianying.service.ShopUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 门店用户信息控制器
 */
@RestController
@RequestMapping("/shopUserInfo")
@Tag(name = "(客户小程序)门店用户信息管理", description = "门店用户信息的增删改查接口")
public class ShopUserInfoController {

    @Autowired
    private ShopUserInfoService shopUserInfoService;

    /**
     * 新增门店用户信息
     */
    @PostMapping("/save")
    @Operation(
            summary = "新增门店用户信息",
            description = "添加门店用户的关联信息，包含所属公司、大区、门店等详细信息",
            parameters = {
                    @Parameter(name = "userId", description = "关联sys_user表的ID", example = "1001", required = true),
                    @Parameter(name = "customerId", description = "关联customer_info表的ID（所属公司）", example = "2001", required = true),
                    @Parameter(name = "regionId", description = "关联region表的ID（所属大区）", example = "4001", required = true),
                    @Parameter(name = "shopId", description = "关联shop_info表的ID（所属门店）", example = "3001", required = true),
                    @Parameter(name = "department", description = "所在部门", example = "技术部", required = false),
                    @Parameter(name = "job", description = "职务", example = "工程师", required = false),
                    @Parameter(name = "realName", description = "姓名", example = "张三", required = true),
                    @Parameter(name = "email", description = "邮箱", example = "zhangsan@example.com", required = false)
            }
    )
    public CommonResult<?> saveShopUserInfo(@RequestBody ShopUserInfo shopUserInfo) {
        return shopUserInfoService.saveShopUserInfo(shopUserInfo);
    }

    /**
     * 更新门店用户信息
     */
    @PostMapping("/update")
    @Operation(
            summary = "更新门店用户信息",
            description = "修改门店用户的关联信息，需包含ID",
            parameters = {
                    @Parameter(name = "id", description = "门店用户信息表的主键ID", example = "1", required = true),
                    @Parameter(name = "userId", description = "关联sys_user表的ID", example = "1001", required = true),
                    @Parameter(name = "customerId", description = "关联customer_info表的ID（所属公司）", example = "2001", required = true),
                    @Parameter(name = "regionId", description = "关联region表的ID（所属大区）", example = "4001", required = true),
                    @Parameter(name = "shopId", description = "关联shop_info表的ID（所属门店）", example = "3001", required = true),
                    @Parameter(name = "department", description = "所在部门", example = "技术部", required = false),
                    @Parameter(name = "job", description = "职务", example = "高级工程师", required = false),
                    @Parameter(name = "realName", description = "姓名", example = "张三", required = true),
                    @Parameter(name = "email", description = "邮箱", example = "zhangsan_new@example.com", required = false)
            }
    )
    public CommonResult<?> updateShopUserInfo(@RequestBody ShopUserInfo shopUserInfo) {
        return shopUserInfoService.updateShopUserInfo(shopUserInfo);
    }

    /**
     * 根据ID删除门店用户信息
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "根据ID删除门店用户信息",
            description = "通过主键ID删除指定的门店用户信息",
            parameters = {
                    @Parameter(name = "id", description = "门店用户信息表的主键ID", required = true, example = "1")
            }
    )
    public CommonResult<?> removeById(@RequestParam Long id) {
        return shopUserInfoService.removeShopUserInfoById(id);
    }

    /**
     * 根据ID查询门店用户信息
     */
    @GetMapping("/getById")
    @Operation(
            summary = "根据ID查询门店用户信息",
            description = "通过主键ID查询门店用户的详细信息",
            parameters = {
                    @Parameter(name = "id", description = "门店用户信息表的主键ID", required = true, example = "1")
            }
    )
    public CommonResult<ShopUserInfo> getById(@RequestParam Long id) {
        return shopUserInfoService.getShopUserInfoById(id);
    }

    /**
     * 根据用户ID查询门店用户信息
     */
    @GetMapping("/getByUserId")
    @Operation(
            summary = "根据用户ID查询门店用户信息",
            description = "通过sys_user表的ID查询关联的门店用户信息",
            parameters = {
                    @Parameter(name = "userId", description = "系统用户表sys_user的主键ID", required = true, example = "1001")
            }
    )
    public CommonResult<ShopUserInfo> getByUserId(@RequestParam Long userId) {
        return shopUserInfoService.getByUserId(userId);
    }

    /**
     * 根据客户ID查询门店用户列表
     */
    @GetMapping("/getByCustomerId")
    @Operation(
            summary = "根据客户ID查询门店用户列表",
            description = "通过customer_info表的ID查询关联的门店用户列表",
            parameters = {
                    @Parameter(name = "customerId", description = "客户信息表customer_info的主键ID", required = true, example = "2001")
            }
    )
    public CommonResult<List<ShopUserInfo>> getByCustomerId(@RequestParam Long customerId) {
        return shopUserInfoService.getByCustomerId(customerId);
    }

    /**
     * 根据门店ID查询门店用户列表
     */
    @GetMapping("/getByShopId")
    @Operation(
            summary = "根据门店ID查询门店用户列表",
            description = "通过shop_info表的ID查询关联的门店用户列表",
            parameters = {
                    @Parameter(name = "shopId", description = "门店信息表shop_info的主键ID", required = true, example = "3001")
            }
    )
    public CommonResult<List<ShopUserInfo>> getByShopId(@RequestParam Long shopId) {
        return shopUserInfoService.getByShopId(shopId);
    }

    /**
     * 分页查询门店用户信息
     */
    @GetMapping("/page")
    @Operation(
            summary = "分页查询门店用户信息",
            description = "分页获取门店用户信息列表，支持多条件筛选",
            parameters = {
                    @Parameter(name = "pageNum", description = "页码，默认1", example = "1", required = false),
                    @Parameter(name = "pageSize", description = "每页条数，默认10", example = "10", required = false),
                    @Parameter(name = "customerId", description = "客户ID", example = "2001", required = false),
                    @Parameter(name = "regionId", description = "大区ID", example = "4001", required = false),
                    @Parameter(name = "shopId", description = "门店ID", example = "3001", required = false),
                    @Parameter(name = "department", description = "部门名称（模糊查询）", example = "技术", required = false),
                    @Parameter(name = "job", description = "职务（模糊查询）", example = "工程师", required = false),
                    @Parameter(name = "realName", description = "姓名（模糊查询）", example = "张", required = false)
            }
    )
    public CommonResult<IPage<ShopUserInfoDTO>> getPageList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) Long regionId,
            @RequestParam(required = false) Long shopId,
            @RequestParam(required = false) String department,
            @RequestParam(required = false) String job,
            @RequestParam(required = false) String realName) {
        return shopUserInfoService.getPageList(pageNum, pageSize, customerId, regionId, shopId, department, job, realName);
    }
}
